package com.chen.q10;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.List;

@SpringBootApplication
public class Application {
    @Autowired
    private JDBCUtil jdbcUtil;

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(Application.class,args);
    }

    @Bean
    public void  work(){
        /*Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://cdb-oj3adz5u.cd.tencentcdb.com:10051/geeklearn";
            String username = "root";
            String password = "cheng#123";
            connection = DriverManager.getConnection(url,username,password);

            statement = connection.createStatement();
            String sql = "INSERT into `user` set id = 1 ,name = '张三'";
            statement.executeUpdate(sql);
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (statement != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }*/
        //insert
        //String sql = "insert into user set id = 3 ,name ='王五'";
        //int rows = jdbcUtil.insertOrUpdate(sql);
        //System.out.println("受影响行数："+ rows);

        //update
//        String sql = "update user set name = '刘六' where id = 3";
//        int rows = jdbcUtil.insertOrUpdate(sql);
//        System.out.println("受影响行数："+ rows);

        //delete
//        String sql = "delete from user where id = 3";
//        int rows = jdbcUtil.insertOrUpdate(sql);
//        System.out.println("受影响行数："+ rows);

        //query
        String sql = "select * from user";
        List<User> userList = jdbcUtil.list(sql,User.class);
        for (User user : userList){
            System.out.println(user);
        }

        //PreparedStatement+批量插入
/*
        Connection conn = null;
        PreparedStatement psmt = null;
        int result = 0;
        try {
            String sql = "insert into user set id = ? , name = ?";
            conn = new jdbcUtil().getConnection();
            psmt = jdbcUtil.getPreparedStatement(conn,false,sql);

            psmt.setInt(1,10);
            psmt.setString(2,"wang10");
            result = result + psmt.executeUpdate();

            psmt.setInt(1,11);
            psmt.setString(2,"wang11");
            result = result + psmt.executeUpdate();

            conn.commit();//提交
            System.out.println("收影响行数："+ result);
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();//回滚
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            //释放资源
            jdbcUtil.release(psmt);
            jdbcUtil.disConnect(conn);
        }
*/
    }
}
